/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.dreamlu.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.querydsl.jpa.impl.JPAQueryFactory;

import net.dreamlu.common.result.Tree;
import net.dreamlu.common.utils.FreemarkerUtil;
import net.dreamlu.db.manager.RoleManager;
import net.dreamlu.db.manager.RoleResourceManager;
import net.dreamlu.db.po.QRole;
import net.dreamlu.db.po.QRoleResource;
import net.dreamlu.db.po.Role;
import net.dreamlu.db.po.RoleResource;
import net.dreamlu.db.qo.RoleQuery;
import net.dreamlu.db.qo.RoleResourceQuery;
import net.dreamlu.mica.core.utils.StringUtil;
import net.dreamlu.system.service.IRoleService;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author L.cm
 * @since 2018-01-29
 */
@Service
public class RoleServiceImpl implements IRoleService {
	@Autowired
	private RoleResourceManager	roleResourceManager;
	@Autowired
	private RoleManager			roleManager;
	@Autowired
	private JdbcTemplate		jdbcTemplate;
	@Autowired
	private JPAQueryFactory		jpaQueryFactory;

	@Override
	public List<Tree> selectTree() {
		List<Tree> trees = new ArrayList<>();
		RoleQuery roleQuery = new RoleQuery();
		roleQuery.setOrderAsc(Role.PROP_SEQ);
		List<Role> roles = roleManager.queryRole(roleQuery);
		for (Role r : roles) {
			Tree tree = new Tree();
			tree.setId(r.getId());
			tree.setText(r.getName());
			tree.setIconCls(r.getIconCls());
			trees.add(tree);
		}
		return trees;
	}

	@Override
	public List<Role> findListByAdminId(Integer adminId) {
		String sql = FreemarkerUtil.sql2String(null, "findListByAdminId.sql");
		RowMapper<Role> rm = BeanPropertyRowMapper.newInstance(Role.class);
		List<Role> roleNameList = jdbcTemplate.query(sql, new Object[] { adminId }, rm);
		return roleNameList;
	}

	@Override
	public void updateRoleResource(Integer roleId, String resourceIds) {
		// 先删除后添加,有点爆力
		RoleResourceQuery roleResourceQuery = new RoleResourceQuery();
		roleResourceQuery.setRoleId(roleId);
		roleResourceManager.deleteRoleResource(roleResourceQuery);
		// 如果资源id为空，判断为清空角色资源
		if (StringUtil.isBlank(resourceIds)) {
			return;
		}
		String[] resourceIdArray = resourceIds.split(",");
		for (String resourceId : resourceIdArray) {
			RoleResource roleResource = new RoleResource();
			roleResource.setRoleId(roleId);
			roleResource.setResourceId(Integer.valueOf(resourceId));
			roleResourceManager.saveRoleResource(roleResource);
		}
	}

	@Override
	public List<Integer> selectResourceIdListByRoleId(Integer id) {
//		String sql = FreemarkerUtil.sql2String(null, "selectResourceIdListByRoleId.sql");
//		RowMapper<Integer> rm = BeanPropertyRowMapper.newInstance(Integer.class);
//		List<Integer> roleNameList = jdbcTemplate.query(sql, new Object[] { id }, rm);
		// queryDsl实现方式
		QRole qrole = QRole.role;
		QRoleResource qRoleResource = QRoleResource.roleResource;
		List<Integer> roleNameList = jpaQueryFactory.select(qRoleResource.resourceId).from(qrole).leftJoin(qRoleResource).on(qrole.id.eq(qRoleResource.roleId))
				.where(qrole.id.eq(id).and(qrole.status.eq(1))).fetch();
		return roleNameList;
	}
}
